package org.asg.dis.controller;

import org.asg.dis.model.ApiResponse;
import org.asg.dis.model.Courier;
import org.asg.dis.model.Shipment;
import org.asg.dis.service.CourierService;
import org.asg.dis.service.ShipmentService;
import org.asg.dis.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;


//TODO Staff 角色的名称是否正确，与角色的配置一致（例如是否是 ROLE_Staff，或者只用 Staff）
// 工作人员拥有快递员管理、快件指派、快件信息查询、投诉信息处理等权限
// 工作人员可以添加、删除、修改快递员信息。
// 工作人员可以修改快件的状态以及快件目前所在位置

@RestController
@RequestMapping("/staff")
@PreAuthorize("hasRole('Staff')")
public class StaffController {
    private final StaffService staffService;
    private final CourierService courierService;
    private final ShipmentService shipmentService;
    @Autowired
    public StaffController(StaffService ss, CourierService cs, ShipmentService shipmentService) {
        this.staffService = ss;
        this.courierService = cs;
        this.shipmentService = shipmentService;
    }

    @PostMapping("/c/add")
    public ApiResponse<?> addCourier(@RequestBody Courier courier) {
        courierService.save(courier);
        //修改营业厅员工数量
        return ApiResponse.ok();
    }

    @PutMapping("/c/modify/{id}")
    public ApiResponse<?> modifyCourierInfo(@PathVariable Long id, @RequestBody Courier modified) {
        Optional<Courier> existingCourier = courierService.findById(id);
        if (existingCourier.isPresent()) {
            courierService.save(modified);
            return ApiResponse.ok();
        } else {
            return ApiResponse.notFound("Courier not found");
        }
    }

    @DeleteMapping("/c/dismiss/{id}")
    public ApiResponse<?> dismissCourier(@PathVariable Long id) {
        Optional<Courier> existingCourier = courierService.findById(id);
        if (existingCourier.isPresent()) {
            courierService.delete(existingCourier.get());
            return ApiResponse.ok();
        } else {
            return ApiResponse.notFound("Courier not found");
        }
    }


    @GetMapping("/shipmentInfo/{id}")
    public ApiResponse<?> queryShipmentInfo(@PathVariable String id) {
        Optional<Shipment> s = shipmentService.getShipmentById(id);
        if(s.isPresent()){
            return ApiResponse.ok(s.get());
        }else{
            return ApiResponse.notFound("指定快件不存在");
        }
    }

    @GetMapping("/shipment/all")
    public ApiResponse<?> all(){
        return ApiResponse.ok( shipmentService.getAllShipments());
    }

    @PostMapping("/circulate/{id}")
    public ApiResponse<?> circulateShipment(@PathVariable String id) {
        return ApiResponse.ok();
    }
}

